package modelo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Index;


@Entity
@Table(name="contato")
public class Contato  implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private long id;
	private String nome;
	private Municipio municipio;
	private List<Telefone> telefones = new ArrayList<Telefone>();

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public long getId() {return id;}
	public void setId(long id) {this.id = id;}
	
	@Column(length=100)
	public String getNome() {return nome;}
	public void setNome(String nome) {this.nome = nome;}
	
	@ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
	@JoinColumn(name="municipio")
	@Fetch(FetchMode.JOIN)
	@ForeignKey(name="municipioKey")
	@Index(name="municipioIndex")
	public Municipio getMunicipio() {return municipio;}
	public void setMunicipio(Municipio municipio) {this.municipio = municipio;}

	@OneToMany (targetEntity=Telefone.class, mappedBy = "contato", fetch = FetchType.LAZY )
	public List<Telefone> getTelefones() {return telefones;}
	public void setTelefones(List<Telefone> telefones) {this.telefones = telefones;}

	@Transient
	public int getTotalTelefone() {
		if(telefones == null)
			return 0;
		return telefones.size();}
	
		
}
